package com.whfc.openapi.api.face.controller;

import com.alibaba.fastjson.JSONArray;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.param.*;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.constant.OpenApiConst;
import com.whfc.openapi.api.face.service.FaceGateSyncService;
import com.whfc.openapi.api.face.util.FaceGateDataConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * @Description 德宝智能-闸机数据同步
 * @Author hw
 * @Date 2021/4/13 15:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/open/api/v1/debao/sync")
public class FaceGateDeBaoController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaceGateSyncService faceGateSyncService;

    @PostMapping("/emp")
    public Result syncEmpList(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO, @RequestBody String body) {
        logger.info("德宝闸机人员同步");
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(() -> {
            List<DeBaoSyncEmpParam> params = JSONArray.parseArray(body, DeBaoSyncEmpParam.class);
            logger.info("德宝闸机人员同步,params:{}", params != null ? params.size() : 0);
            if (params == null || params.isEmpty()) {
                return;
            }
            Integer deptId = clientDTO.getDeptId();
            for (DeBaoSyncEmpParam param : params) {
                EmpInfoSyncDataParam dataParam = FaceGateDataConverter.empInfoDataConverter(param);
                dataParam.setDeptId(deptId);
                dataParam.setPlatform(FaceGateType.DEBAO.getCode());
                faceGateSyncService.syncEmpInfo(dataParam);
            }
        });

        return ResultUtil.success();
    }

    @PostMapping("/group")
    public Result syncGroup(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO, @RequestBody String body) {
        logger.info("德宝闸机班组同步:body:{}", body);
        List<DeBaoSyncGroupParam> params = JSONArray.parseArray(body, DeBaoSyncGroupParam.class);
        logger.info("德宝闸机班组同步:params:{}", params != null ? params.size() : 0);
        if (params == null || params.isEmpty()) {
            return ResultUtil.success();
        }
        Integer deptId = clientDTO.getDeptId();
        for (DeBaoSyncGroupParam param : params) {
            EmpGroupSyncDataParam dataParam = FaceGateDataConverter.groupDataConverter(param);
            if (dataParam != null) {
                dataParam.setDeptId(deptId);
                dataParam.setPlatform(FaceGateType.DEBAO.getCode());
                faceGateSyncService.syncGroup(dataParam);
            }
        }
        return ResultUtil.success();
    }

    @PostMapping("/attend")
    public Result syncAttend(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO, @RequestBody String body) {
        logger.info("德宝闸机考勤同步");
        Integer deptId = clientDTO.getDeptId();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(() -> {
            List<FaceGateRecordParam> params = JSONArray.parseArray(body, FaceGateRecordParam.class);
            logger.info("德宝闸机考勤同步:params:{}", params != null ? params.size() : 0);
            if (params == null || params.isEmpty()) {
                return;
            }
            //按时间排序
            params = params.stream()
                    .filter(param -> param.getShowTime() != null)
                    .sorted(Comparator.comparing(FaceGateRecordParam::getShowTime))
                    .collect(Collectors.toList());
            for (FaceGateRecordParam param : params) {
                EmpAttendSyncDataParam dataParam = FaceGateDataConverter.attendDataConverter(param);
                if (dataParam != null) {
                    dataParam.setDeptId(deptId);
                    dataParam.setPlatform(FaceGateType.DEBAO.getCode());
                    faceGateSyncService.syncAttend(dataParam);
                }
            }
        });
        return ResultUtil.success();
    }

}
